/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils.easing
{
	public class BouncingEasing extends Easing implements IEasing
	{
		private var _start:Number;
		private var _dist:Number;
		private var _n_pi:Number;
		private var _firstCount:uint;
		private var _goalCount:uint;
		/**
		 * Bouncing Easing 
		 * @param start Start number
		 * @param end End number
		 * @param count The number of frames
		 * @param bounce The number of times to cross over the end number
		 * @param firstCountAtGoal if <code>true</code>, <code>count</code> is the first count when it go over the goal. Otherwise, <code>count</code> is where it stops. Default is <code>false</code>
		 */
		public function BouncingEasing(start:Number, end:Number, count:uint, bounce:Number, firstCountAtGoal:Boolean = false)
		{
			if (bounce < 0) {
				throw ArgumentError("bounce should be a positive number");
			}
			frame = firstCountAtGoal ? count*(bounce+1) : count;
			_n_pi = (bounce+1) * Math.PI;
			_dist = end - start;
			_start = start;
			initialize();
			_goalCount = frame;
			_firstCount = frame / (bounce+1);
		}

		public function forward():Number
		{
			pos += isMoving() ? 1 : 0;
			var x:Number = pos/frame;
			return ((1-1/x)*Math.sin(_n_pi * x) / _n_pi + 1)*_dist + _start;
		}
		
		public function get goalFrame():uint {
			return _goalCount;
		}
		
		public function get firstFrameAtGoal():uint {
			return _firstCount;
		}
	}
}